//
//  OASelectPersonController.m
//  OASystem
//
//  Created by zhoupengfei on 15/5/3.
//  Copyright (c) 2015年 zpf. All rights reserved.
//
#import "OADepartmentModel.h"
#import "OAExpendViewController.h"
#import "OASelectPersonController.h"
#import "OADepartmentModel.h"
#import "HttpTools.h"
#import "MBProgressHUD.h"
#import "GDataDefines.h"
#import "Toast+UIView.h"
#import "OACheckBoxFootView.h"


@interface OASelectPersonController ()
@property(nonatomic,weak)OACheckBoxFootView * checkBoxFootView;
@property(nonatomic,strong)UIBarButtonItem * rightItem;
@end

@implementation OASelectPersonController

- (void)viewDidLoad {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getSelectName:) name:@"daiban" object:nil];

    [super viewDidLoad];
    [self setupRightItems];
       UIView *view = [[UIView alloc]init];
    
    view.frame = CGRectMake(0, KScreenHeight-80, KScreenWidth,80);
//view.backgroundCo lor = [UIColor redColor];
        UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(0, 40, KScreenWidth/2,40);
    [button1 setTitle:@"钉钉提醒" forState:UIControlStateNormal];
    button1.selected = YES;
    _remind = @"1";
        _remind2 = @"0";

    [button1 setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
        button1.titleLabel.font = [UIFont systemFontOfSize: 13.0];
    [button1 addTarget:self action:@selector(midaction1:) forControlEvents:UIControlEventTouchUpInside];

    [button1 setImage:[UIImage imageNamed:@"shopping_xuanzhe_default"] forState:UIControlStateNormal];
        [button1 setImage:[UIImage imageNamed:@"shopping_xuanzhe_highlighted"] forState:UIControlStateSelected];
       [button1 layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft
                                    imageTitleSpace:20];
            UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];

       button2.frame = CGRectMake(button1.right, 40, KScreenWidth/2,40);
  button2.titleLabel.font = [UIFont systemFontOfSize: 13.0];
    [button2 addTarget:self action:@selector(midaction2:) forControlEvents:UIControlEventTouchUpInside];

    [button2 setTitle:@"重要消息提醒" forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
    [button2 setImage:[UIImage imageNamed:@"shopping_xuanzhe_default"] forState:UIControlStateNormal];
        [button2 setImage:[UIImage imageNamed:@"shopping_xuanzhe_highlighted"] forState:UIControlStateSelected];
       [button2 layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft
                                    imageTitleSpace:20];
        UIView *view2 = [[UIView alloc]init];
    view2.frame = CGRectMake(0, button1.bottom, KScreenWidth, 40);
    view2.backgroundColor  = [UIColor yellowColor];
_checkBoxFootView = [OACheckBoxFootView checkBoxFootView];
view2 = _checkBoxFootView;
[view addSubview: button1];
[view addSubview: button2];

[view addSubview: view2];
            self.tableView.tableFooterView = view;
//    OACheckBoxFootView * checkBoxFootView = [OACheckBoxFootView checkBoxFootView];
//    self.tableView.tableFooterView = checkBoxFootView;
//    self.checkBoxFootView = checkBoxFootView;
        _operationItem.enabled = YES;

}
- (void)midaction1:(UIButton *)button{
            button.selected = !button.isSelected;
if (button.selected) {
    _remind = [NSString stringWithFormat:@"%d",1];
}else{
    _remind = @"0";


}

}
- (void)midaction2:(UIButton *)button{
            button.selected = !button.isSelected;
if (button.selected) {
    _remind2 = [NSString stringWithFormat:@"%d",2];
}else{
    _remind2 = @"0";


}

}
-(void)setupRightItems{
    _operationItem = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(operationACtion)];
     _operationItem2 = [[UIBarButtonItem alloc]initWithTitle:@"选择树" style:UIBarButtonItemStylePlain target:self action:@selector(operationACtion2)];
         [self.navigationItem setRightBarButtonItems:@[_operationItem,_operationItem2]];

//    self.navigationItem.rightBarButtonItem = _operationItem;
    
}
-(void)getSelectName:(NSNotification*)note{
    
    NSArray * array = note.userInfo[@"SelectName"];
    NSMutableArray *mutarray = [NSMutableArray array];
    mutarray= _personArray;
    for (NSString *string  in array) {
    OADepartmentModel *model = [OADepartmentModel alloc];
    model.department= string;
    model.isSelected = YES;
    [_personArray addObject:model];
}
[self.tableView reloadData];
//       NSString *string= [array componentsJoinedByString:@","];

//   [ self.jybutton setTitle:string forState:UIControlStateNormal] ;
    
}
- (void)operationACtion2{
         OAExpendViewController * expendVC = [[OAExpendViewController alloc] init];
        expendVC.type = UnitDataType;
        expendVC.types = @"daiban";
        [self.navigationController pushViewController:expendVC animated:YES];
    }
#pragma mark 提交
-(void)operationACtion{
NSMutableArray * tempArray = [NSMutableArray array];
    for (OADepartmentModel * model in self.personArray) {
        if (model.isSelected) {
            [tempArray addObject:model.department];
        }
    }
if (tempArray.count == 0) {
        [self.view makeToast:@"请选择人员"];

        return;
    
    }

    else{

 _remind3 = [NSString stringWithFormat:@"%@,%@",_remind,_remind2];

    [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    self.rightItem.enabled = NO;

    NSString * selectPersons = [tempArray componentsJoinedByString:@","];
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array = @[@{@"unid":self.fileId},@{@"userId":userId},@{@"segment":self.setpString},@{@"staff":selectPersons},@{@"ryorder":self.checkBoxFootView.orderString},@{@"famxml":self.body},@{@"remind":_remind3}];
    [HttpTools postWithURL:KexchdataURL arrays:array method:KSetStaffListDeliVerMethod success:^(id JSON) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        self.rightItem.enabled = YES;
        GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData: [JSON dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
        GDataXMLElement *rootElement = [doc rootElement];
        NSArray *arrData= [rootElement elementsForName:@"info"];
        NSString * message= nil;
        for (GDataXMLElement * subXMLElement in arrData) {
            GDataXMLElement * element = [[subXMLElement elementsForName:@"msg"] objectAtIndex:0];
            message  = [element stringValue];
        }
        
    if ([message length ]==2) {
        self.rightItem.enabled = NO;

       [self.view makeToast:@"发送成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshOADetailData" object:nil];
            [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
}else{
        [self.view makeToast:@"发送失败"];

}
        
    
        
    } failure:^(NSError *error) {
        self.rightItem.enabled = YES;
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        [self.view makeToast:@"发送失败"];
    }];
}
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.personArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * cellIdentifier = @"cellIdentifier";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell ==nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    OADepartmentModel * model = self.personArray[indexPath.row];
    cell.textLabel.text = model.department;
    if (model.isSelected) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    OADepartmentModel * model = self.personArray[indexPath.row];
    model.isSelected = !model.isSelected;
    [self.tableView reloadData];
    
}


@end

